@extends('backend::layout')

@section('title', '干预训练记录列表')

@section('content')
    <div class="card border-no">

        <div class="card-body">
            <form method="get">
                <div class="row">
                    <div class="clearfix"></div>
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <label class="col-md-2 col-sm-12">
                            <input class="form-control" type="text" placeholder="学生姓名" name="child_name" value="{{ old("child_name") ?: (isset($query['child_name']) ? $query['child_name'] : '') }}" />
                        </label>
                        @if($isSchool)
                            <input type="hidden" name="agency_id" value="{{$admin->agency_id}}" />
                            <label class="col-md-2 col-sm-12">
                                <select class="form-control" name="user_id">
                                    <option  value="">--{{__("Teacher")}}--</option>
                                    @foreach($users as $val)
                                        <option value="{{ $val->id }}" @if(old('user_id') === $val->id || (old('user_id') === null && (isset($query['user_id']) && $query['user_id'] ==  $val->id )) ) selected @endif >{{ strlen($val->name) ? $val->name : $val->phone  }}</option>
                                    @endforeach
                                </select>
                            </label>
                        @else
                            <label class="col-md-2 col-sm-12">
                                <select class="form-control" id="agency" name="agency_id">
                                    <option  value="">--{{__("Agency")}}--</option>
                                    @foreach($agencies as $val)
                                        <option value="{{ $val->id }}" @if(old('agency_id') === $val->id || (old('agency_id') === null && (isset($query['agency_id']) && $query['agency_id'] ==  $val->id )) ) selected @endif >{{ $val->name }}</option>
                                    @endforeach
                                </select>
                            </label>

                            <label class="col-md-2 col-sm-12">
                                <select class="form-control" id="user" name="user_id">
                                    <option  value="">--{{__("Teacher")}}--</option>
                                    @foreach($users as $val)
                                        <option value="{{ $val->id }}" @if(old('user_id') === $val->id || (old('user_id') === null && (isset($query['user_id']) && $query['user_id'] ==  $val->id )) ) selected @endif >{{ strlen($val->name) ? $val->name : $val->phone }}</option>
                                    @endforeach
                                </select>
                            </label>
                        @endif
                        <label class="col-md-2 col-sm-12">
                            <input type="text" placeholder="起始时间" name="startquerytime" class="form-control datepicker" readonly value="{{ isset($query['startquerytime']) ? $query['startquerytime'] : '' }}">
                        </label>


                        <label class="col-md-2 col-sm-12">
                            <input type="text" placeholder="结束时间" name="endquerytime" class="form-control datepicker" readonly="readonly" value="{{ isset($query['endquerytime']) ? $query['endquerytime'] : '' }}">
                        </label>
                        <label class="col-md-2 col-sm-12">
                            <button class="btn btn-primary btn-sm">搜索</button>
                        </label>
                    </div>
                </div>
            </form>

            {{--<h4 class="text-black f-20">干预训练记录列表</h4>--}}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">学生姓名</th>
                        <th scope="col">干预训练项目</th>
                        <th scope="col">项目等级</th>
                        @if($isSchool)
                        <th scope="col">使用设备编码</th>
                        @endif
                        <th scope="col">开始时间</th>
                        <th scope="col">完成时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    @if(isset($list))
                        @foreach($list as $idx => $item)
                            <tr>
                                <th scope="row">{{ $idx + 1 }}</th>
                                <td>
                                    @if($item->child)
                                        {{ $item->child->name }}
                                    @else
                                        <span class="label label-warning"> 已删除</span>
                                    @endif
                                </td>
                                <td>{{ $item->game->name }}</td>
                                <td>{{ $item->grade->name }}</td>
                                @if($isSchool)
                                <td>{{ $item->device->code }}</td>
                                @endif
                                <td>{{ $item->start_time }}</td>
                                <td>{{ $item->end_time }}</td>
                                <td>
                                    @if( $currentAdmin->allow('training/view'))
                                        <a href="{{ route('admin.training.view', ['id' => $item->id]) }}" class="text-primary" title="{{ __('View') }}">&nbsp;&nbsp;<i class="fa fa-eye"></i></a>
                                    @endif
                                    @if($currentAdmin->allow('training/delete'))
                                        <a href="{{ route('admin.training.delete', ['id' => $item->id]) }}" class="text-danger confirm-delete" title="{{ __('Delete') }}">&nbsp;&nbsp;<i class="fa fa-trash-o"></i></a>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    @endif
                    </tbody>
                </table>
            </div>
            <div class="row">
                <div class="clearfix"></div>
                <div class="col-md-12 col-sm-12 col-xs-12 pull-right">
                    @if(isset($list))
                        {{ $list->links() }}
                    @endif
                </div>
            </div>
        </div>
    </div>
@stop

@section('script')

<script>
    $("document").ready(function (){

        $('.datepicker').datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd',
            minView:2,
        });

        $("#agency").change(function (){
            console.log($(this).val());
            if ($(this).val() > 0){

                $.getJSON("{{ route("admin.user.ajaxAgencyUser") }}", {agency_id : $(this).val()}, function (data){
                    console.log(data);
                    console.log(data.status);
                    $("#user").empty();
                    if (data.status != 200){

                        console.log(data.status);
                    }else{
                        let str = "<option  value=\"\">--{{__("Teacher")}}--</option>";
                        for (let i in data.users){
                            if (data.users[i].name.length > 0){
                                str += "<option value=\"" + data.users[i].id + "\">" + data.users[i].name + "</option>";
                            }else {
                                str += "<option value=\"" + data.users[i].id + "\">" + data.users[i].phone + "</option>";
                            }

                        }
                        $("#user").append(str);

                    }
                });
            }else{
                $("#user").html("<option  value=\"\">--{{__("Teacher")}}--</option>");
                $("#user").find("option").eq(0).prop("selected",true);
            }
        });
    });
</script>
@stop